package com.yanqu.road.server.manger.activity.snowgarden.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.ServerMallBuyProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.snowgarden.ActivityMallDayBuyMgr;
import com.yanqu.road.server.manger.activity.snowgarden.MallDayBuyActivity;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.List;

public class ActivityMallDayBuyRefreshServiceCmd implements IGameServiceCommand {
    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        ServerMallBuyProto.CrossMallBuyToServerSyncMsg syncMsg = ServerMallBuyProto.CrossMallBuyToServerSyncMsg.parseFrom(request.getDataBytes());

        MallDayBuyActivity activity = ActivityMallDayBuyMgr.getActivity(syncMsg.getActivityId());
        if (activity != null) {
            activity.setReceiveCross(true);
            for (ServerMallBuyProto.DayBuyDataTemp dayBuyDataTemp : syncMsg.getCrossBuyDataList()) {
                activity.updateActivityTotalCount(dayBuyDataTemp.getDay(), dayBuyDataTemp.getMallId(), dayBuyDataTemp.getNewCount());
            }
            if (!syncMsg.getCrossBuyDataList().isEmpty()) {
                List<GamePlayer> allOnlinePlayer = GamePlayerMgr.getAllOnlinePlayer();
                for (GamePlayer player : allOnlinePlayer) {
                    player.notifyListener(eGamePlayerEventType.SeasonServerMallBuyTask.getValue());
                }
            }
        }

        return syncResponse;
    }
}
